package jin.chen.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jin.chen.pojo.*;
import jin.chen.pojo.vo.*;
import jin.chen.service.CarouselService;
import jin.chen.service.CategoryService;
import jin.chen.service.ItemService;
import jin.chen.utils.PagedResult;
import jin.chen.utils.Result;
import jin.chen.utils.enums.CarouselShowEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品展示的controller
 */
@Api(value = "商品展示", tags = "商品展示controller")
@RestController
@RequestMapping("/items")
public class ItemController  extends BaseController {
    @Autowired
    private ItemService itemService;

    @ApiOperation(value = "获取商品详情接口", notes = "获取商品详情接口", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public Result subCat(@ApiParam(name = "itemId", value = "商品id", required = true)
                             @PathVariable String itemId){
        if (StringUtils.isBlank(itemId)){
            return Result.errorMsg("商品不存在");
        }
        Items item = itemService.queryItemById(itemId);
        List<ItemsImg> listImg = itemService.queryItemImgList(itemId);
        List<ItemsSpec> listSpec = itemService.queryItemSpecList(itemId);
        ItemsParam itemsParam = itemService.queryItemParam(itemId);
        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(item);
        itemInfoVO.setItemImgList(listImg);
        itemInfoVO.setItemSpecList(listSpec);
        itemInfoVO.setItemParams(itemsParam);
        return Result.OK(itemInfoVO);
    }

    @ApiOperation(value = "查询商品评价等级接口", notes = "查询商品评价等级接口", httpMethod = "GET")
    @GetMapping("/commentLevel")
    public Result commentLevel(@ApiParam(name = "itemId", value = "商品id", required = true)
                         @RequestParam String itemId){
        if (StringUtils.isBlank(itemId)){
            return Result.errorMsg("商品不存在");
        }
        CommentLevelCountsVO countsVO = itemService.queryCommentCounts(itemId);
        return Result.OK(countsVO);
    }

    @ApiOperation(value = "查询商品评论接口", notes = "查询商品评论接口", httpMethod = "GET")
    @GetMapping("/comments")
    public Result commentLevel(@ApiParam(name = "itemId", value = "商品id", required = true)
                               @RequestParam String itemId,
                               @ApiParam(name = "level", value = "商品评价等级", required = false)
                               @RequestParam Integer level,
                               @ApiParam(name = "page", value = "查询的第几页", required = false)
                                   @RequestParam Integer page,
                               @ApiParam(name = "pageSize", value = "每一页显示的条数", required = false)
                                   @RequestParam Integer pageSize){
        if (StringUtils.isBlank(itemId)){
            return Result.errorMsg("商品不存在");
        }
        if (page == null){
           page = 1;
        }
        if (pageSize == null){
           pageSize = COMMENT_PAGE_SIZE;
        }
        PagedResult result = itemService.queryItemPageComments(itemId, level, page, pageSize);
        return Result.OK(result);
    }

    /**
     * 搜索商品接口
     * @param keywords  搜索的关键字
     * @param sort   排序方式，名称，价格，销量
     * @param page   当前页数
     * @param pageSize   每页显示的条目数
     * @return
     */
    @ApiOperation(value = "搜索商品接口", notes = "搜索商品接口", httpMethod = "GET")
    @GetMapping("/search")
    public Result search(@ApiParam(name = "keywords", value = "关键字", required = true)
                               @RequestParam String keywords,
                               @ApiParam(name = "sort", value = "排序方式", required = false)
                               @RequestParam String sort,
                               @ApiParam(name = "page", value = "查询的第几页", required = false)
                               @RequestParam Integer page,
                               @ApiParam(name = "pageSize", value = "每一页显示的条数", required = false)
                               @RequestParam Integer pageSize){
        if (StringUtils.isBlank(keywords)){
            return Result.errorMsg("关键字不存在");
        }
        if (page == null){
            page = 1;
        }
        if (pageSize == null){
            pageSize = SEARCH_PAGE_SIZE;
        }
        PagedResult result = itemService.searchItems(keywords, sort, page, pageSize);
        return Result.OK(result);
    }


    /**
     * 搜索商品接口
     * @param catId  搜索的关键字
     * @param sort   排序方式，名称，价格，销量
     * @param page   当前页数
     * @param pageSize   每页显示的条目数
     * @return
     */
    @ApiOperation(value = "通过三级分类id搜索商品接口", notes = "通过三级分类id搜索商品接口", httpMethod = "GET")
    @GetMapping("/catItems")
    public Result catItems(@ApiParam(name = "catId", value = "三级分类id", required = true)
                         @RequestParam Integer catId,
                         @ApiParam(name = "sort", value = "排序方式", required = false)
                         @RequestParam String sort,
                         @ApiParam(name = "page", value = "查询的第几页", required = false)
                         @RequestParam Integer page,
                         @ApiParam(name = "pageSize", value = "每一页显示的条数", required = false)
                         @RequestParam Integer pageSize){
        if (catId == null){
            return Result.errorMsg("分类不存在");
        }
        if (page == null){
            page = 1;
        }
        if (pageSize == null){
            pageSize = SEARCH_PAGE_SIZE;
        }
        PagedResult result = itemService.searchItemsByCatId(catId, sort, page, pageSize);
        return Result.OK(result);
    }

    @ApiOperation(value = "根据商品规格ids查询最新数据接口", notes = "根据商品规格ids查询最新数据接口", httpMethod = "GET")
    @GetMapping("/refresh")
    public Result refresh(@ApiParam(name = "itemSpecIds", value = "商品规格ids", required = true)
                        @RequestParam String itemSpecIds){
        if(StringUtils.isBlank(itemSpecIds)){
            return Result.errorMsg("");
        }
        List<ShopCatVO> list = itemService.queryItemsBySpecIds(itemSpecIds);
        return Result.OK(list);
    }
}
